<template>
  <view class="admin">
    <pageHeader
      ref="header"
      :isToggle="isToggle"
      :data="data"
      @getInfo="getInfo"
    ></pageHeader>
    <pageContent
      v-if="info"
      :data="data"
      :info="info"
      :rules="rules"
      :isToggle="isToggle"
      @isToggle="changeIsToggle"
    >
      <image
        class="imageArr"
        v-if="asyncImgs && asyncImgs.store_m_admin && !isToggle"
        :src="asyncImgs.store_m_admin.home_arr"
        mode="widthFix"
        @click="isToggle = true"
      ></image>
    </pageContent>
	
	<!-- 多店铺弹窗选择店铺 -->
	<u-popup :mask-close-able="false" v-model="show" mode="center"  border-radius="14" width="620rpx" height ="620rpx">
		<view class="selectMchStore">
			<scroll-view scroll-y="true" style="height: 100%">
				<view v-for="(item,index) in  mchList" :key="index">
					<view class="flex">
						<u-image border-radius="16rpx" width="100rpx" height="100rpx" :src="item.logo"></u-image>
						<view class="msg">
							<view class="name">
								{{item.name}}
							</view>
							<view class="address">
								{{item.address}}
							</view>
						</view>
						<view class="right">
							<view class="btn" @click="goMchStore(item.id)">
								进入
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</u-popup>
	
	
	
	
  </view>
</template>

<script>
import pageHeader from "./components/page-header.vue";
import pageContent from "./components/page-content.vue";
import { storeAdminStoreInfo } from "@/api/admin";

export default {
  components: {
    pageHeader,
    pageContent,
  },

  data() {
    return {
		isMenusCut: false, // 用于标记是否已经截取过
		mch_select:0,
		show:false,
		mchList:[],
		active:null,
		is_mch:null,
      style: {
        opacity: 0,
        top: 0,
      },
	  img:'https://chidian.cyyvip.com/web/uploads/images/store_1/2022-12-09/f24dbddc75257c97db8de177fe9aa3b254843242.png',

      isToggle: false,

      info: null,

      data: {
        orderList: [
          {
            url: "/admin/order/list/index?id=1",
            num: 0,
            name: this.$t("person.status1"),
            id: 1,
            is: true,
          },
          {
            url: "/admin/order/list/index?id=2",
            num: 0,
            name: this.$t("person.status2"),
            id: 2,
            is: true,
          },
          {
            url: "/admin/order/afterSales/index?id=0",
            num: 0,
            name: this.$t("newAdmin.order.salesService"),
            id: 3,
            is: true,
          },
          // { url: "", num: 0, name: "待评价", id: 4, is: false },
          {
            url: "/admin/order/afterSales/index?id=1",
            num: 0,
            name: this.$t("admin.pending"),
            id: 5,
            is: true,
          },
        ],

        menus: [],

        total: null,

        store: null,

        realTime: [
          { num: 0, name: this.$t("newAdmin.home.home1") },
          { num: 0, name: this.$t("newAdmin.home.home2") },
          { num: 0, name: this.$t("newAdmin.home.home3") },
          { num: 0, name: this.$t("newAdmin.home.home4") },
          { num: 0, name: this.$t("newAdmin.home.home5") },
          { num: 0, name: this.$t("newAdmin.home.home6") },
          { num: 0, name: this.$t("newAdmin.home.home7") },
          { num: 0, name: this.$t("newAdmin.home.home8"), isMore: true },
        ],

        notice: [],
      },

      rules: [],
    };
  },

  onLoad(option) {
	  if(option.is_mch){
		  this.is_mch=option.is_mch
		  uni.setStorageSync('is_mch',this.is_mch)
	  }
    this.$utils.hideShareMenu();
    this.setMenuList();
  },

  onShow() {
    this.getData();
  },
  

  methods: {
	  // 进入入驻商店铺
    goMchStore(id){
	     this.mch_select=id
		 this.show=false
		 this.getData()
    },
    changeIsToggle(isToggle) {
      this.isToggle = isToggle;
    },

    getInfo(info) {
      this.info = info;
    },

    setMenuList() {
      this.$nextTick(() => {
        let {
          home_shop,
          home_order,
          home_sh,
          home_category,
          home_data,
          home_pj,
          home_goods,
          erp_scan
        } = this.asyncImgs.store_m_admin;
		let {
		 cash,
		 qr,
		 store
		} = this.asyncImgs.mch_m_admin;

        this.data.menus = [
          {
            img: home_goods,
            is: "1",
            name: "商品",
            url: "/admin/goods/list/index",
            key: "goods",
          },
          {
            img: home_order,
            is: "1",
            name: "订单",
            url: "/admin/order/list/index",
            key: "order",
          },
          {
            img: home_sh,
            is: "1",
            name: "售后",
            url: "/admin/order/afterSales/index",
            key: "refund_order",
          },
          {
            img: home_category,
            is: "1",
            name: "分类",
            url: "/admin/goods/category/index",
            key: "category",
          },
          {
            img: home_pj,
            is: "1",
            name: "商品评价",
            url: "/admin/evaluate/list/index",
            key: "evaluate",
          },
          {
            img: home_data,
            is: "1",
            name: "店铺数据",
            url: `/admin/data/index?is_mch=${this.is_mch}`,
            key: "store_data",
          },
          {
            img: home_shop,
            is: "1",
            name: "店铺",
            url: "/admin/shop/index/index",
            key: "store",
          },
          {
            img: erp_scan,
            is: "1",
            name: "扫码入库",
            url: "/goods/erpScan/index/index",
            key: "erp_scan",
          },
		  {
		    img: store,
		    is: "1",
		    name: "店铺管理",
		    url: "/admin/storeManagement/storeManagement",
		    key: "store_data",
		  },
		  {
		    img: cash,
		    is: "1",
		    name: "账户提现",
		    url: "/admin/accountWithdrawal/accountWithdrawal",
		    key: "store_data",
		  },
		  {
		    img: qr,
		    is: "1",
		    name: "推广码",
		    url: "/admin/promoteCode/promoteCode",
		    key: "store_data",
		  },
		  {
		    img: this.img,
		    is: "1",
		    name: "扫码核销",
		    url: "scanCodeVerification",
		    key: "store_data",
		  },
        ]
      });
	 
    },

    getData() {
      storeAdminStoreInfo({is_mch:this.is_mch,mch_select:this.mch_select})
        .then((res) => {
			if(this.is_mch!=1){
				const menusCopy = JSON.parse(JSON.stringify(this.data.menus));
				const length = menusCopy.length;
				const startIndex = length - 4;
				if (startIndex >= 0 &&!this.isMenusCut) {
					menusCopy.splice(startIndex, 4);
				}
				this.data.menus = menusCopy;
				this.isMenusCut = true;
				
			}
			console.log('我执行了');
			
          uni.stopPullDownRefresh();
          if (res.code === 0) {
            let { order, notice, store, total, rules } = res.data,
              {
                pay_order_count,
                send_order_count,
                after_sale_order_count,
                comment_order_count,
                sale_order_count,
              } = order,
              { orderList } = this.data;

            if (order) {
              this.$set(orderList[0], "num", pay_order_count || 0);
              this.$set(orderList[1], "num", send_order_count || 0);
              this.$set(orderList[2], "num", after_sale_order_count || 0);
              // this.$set(orderList[3], "num", comment_order_count || 0);

              // 上面的 注释回来 需要修改索引
              this.$set(orderList[3], "num", sale_order_count || 0);
            }

            this.rules = rules;

            if (notice) {
              this.data.notice = notice;
            }

            if (store) {
              this.data.store = store;
            }

            if (total) {
              this.data.total = total;
            }
			if(res.data.mch.code==0){
				this.active==0
				this.data.store = res.data.mch.data;
				console.log('走到了mch')
			}
          } else if (res.code === 1 && res.msg === "非商城管理员。") {
            uni.navigateBack({
              delta: 1,
            });
          }
        })
        .catch((req) => {
			if(req.code==2){
				this.show=true
				this.mchList=req.data
				console.log('我是多店铺',this.mchList)
			}
			if(req.code==5){
				uni.reLaunch({
				  url: "/admin/mchRegister/mchRegister"
				});
			}
          uni.stopPullDownRefresh();
        });
    },
  },

  onPullDownRefresh() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.admin {
  background: #f8f8f8;
  min-height: 100vh;
  overflow: hidden;
}

.imageArr {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateY(-16rpx) translateX(-50%);
  width: 56rpx;
}
.selectMchStore{
	padding: 24rpx;
	.flex{
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		.msg{
			margin-left: 20rpx;
			.name{
				font-weight: 600;
				font-size: 30rpx;
				color: #262626;
			}
			.address{
				font-weight: 400;
				font-size: 24rpx;
				color: #8C8C8C;
				margin-top: 15rpx;
			}
		}
		.right{
			flex-grow: 1;
			display: flex;
			justify-content: flex-end;
			.btn{
				width: 100rpx;
				height: 52rpx;
				background: #F0250E;
				border-radius: 10rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-weight: 600;
				font-size: 26rpx;
				color: #FFFFFF;
			}
		}
	}
}
</style>
